﻿using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Xml;
using LitJson;
using MediaPortal.GUI.Library;
using System.Threading;
using System.Net;
using System.ComponentModel;
using System.IO;
using System.Globalization;

namespace MediaPortal.Plugins.Trailers.Providers
{
    public class MovieMazeDetailsPage
    {

        private const string trailerRegExp = "<td.*?>(?<resolution>\\d*? x \\d*?)<\\/td><td.*?>(?<filesize>.*?)<\\/td><td.*?>.*?<\\/td><td.*?>.*?(?<url>\\/media\\/trailer\\/delivery\\/.*?)\".*?<\\/td>";
        private string html = string.Empty;

        public MovieMazeDetailsPage(string url)
        {
            MovieMazeTrailerWebClient client = new MovieMazeTrailerWebClient();
            client.GetWebPage(MovieMazeTrailers.urlBase + url, MovieMazeTrailers.urlBase, out html);
        }
        
        public string getRating()
        {
            string ratingRegExp = "<img src=\"\\/filme\\/grafiken\\/bewertung\\/(?<rating>\\d*?)\\.gif";

            int result = 0;
            int.TryParse(Regex.Match(this.html, @ratingRegExp).Groups["rating"].Value.ToString().Trim(), out result);

            return "" + (result / 10);
        }

        public DateTime getReleaseDate_DE()
        {
            string releaseRegExp = "<span.*?>Dt.Start:<\\/span>.*?<nobr>(?<releasedate>.*?)<\\/nobr>";

            String releaseDateString = Regex.Match(this.html, @releaseRegExp, RegexOptions.Singleline).Groups["releasedate"].Value.ToString().Trim();

            DateTime releaseDate = DateTime.MinValue;
            try
            {
                releaseDate = DateTime.Parse(releaseDateString, new CultureInfo("de-DE"));
            }
            catch (Exception e)
            {
                Log.Debug("[MyTrailers][MovieMaze Trailers] Error parsing release date: '{0}'", releaseDateString);
            }

            return releaseDate;
        }

        public String getDirector()
        {
            string dircetorRegExp = "<span.*?>Regie:<\\/span>.*?<td.*?>(?<director>.*?)<\\/td>";

            String director = Regex.Match(this.html, @dircetorRegExp, RegexOptions.Singleline).Groups["director"].Value.ToString().Trim();
            
            MovieMazeTrailerWebClient client = new MovieMazeTrailerWebClient();

            return client.StripHTML(director);
        }

        public void getCast(List<string> castList)
        {
            string dircetorRegExp = "<span.*?>Darsteller:<\\/span>.*?<td.*?>(?<cast>.*?)<\\/td>";
            String cast = Regex.Match(this.html, @dircetorRegExp, RegexOptions.Singleline).Groups["cast"].Value.ToString();
            MovieMazeTrailerWebClient client = new MovieMazeTrailerWebClient();

            if (!string.IsNullOrEmpty(cast))
            {
                foreach (string ca in cast.Split(','))
                {
                    Log.Debug("[MyTrailers][MovieMaze Trailers] Cast: '{0}'", ca.Trim());
                    castList.Add(client.StripHTML(ca.Trim()));
                }
            }
        }
    }
}